﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLKDS.DTO;

namespace QLKDS.DAL
{
    public class NationalManager
    {
        public NationalInfo GetNationalById(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var queryNational = from d in db.Nationals
                                    where d.Id.Equals(id)
                                    select new NationalInfo { Id = d.Id,  Name = d.Name,Description=d.Description};

                return queryNational.First();
            }
        
        }


        public IList<NationalInfo> ListNational()
        {
            using (var db = new QLKDSDataContext())
            {
                var queryNational = from d in db.Nationals
                                   select new NationalInfo { Id = d.Id,Name = d.Name,Description=d.Description };

                return queryNational.ToList();
            }
        }
    

        public void CreateNational(National National)
        {
            using (var db = new QLKDSDataContext())
            {
                db.Nationals.InsertOnSubmit(National);

                db.SubmitChanges();
               
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="National"></param>
        public void UpdateNational(National National)
        {
            using (var db = new QLKDSDataContext())
            {
                var _National = db.Nationals.Single(d => d.Id.Equals(National.Id));

                _National.Name = National.Name;
                _National.Description = National.Description;
                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public void DeleteNational(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var _National = db.Nationals.Single(d => d.Id.Equals(id));

                db.Nationals.DeleteOnSubmit(_National);
                db.SubmitChanges();
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool HasNameExisted(string name)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Nationals
                            where d.Name.Equals(name)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="code"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool HasNameDuplicated(string name, long id)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Nationals
                            where d.Name.Equals(name)
                            where !d.Id.Equals(id)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

     
    }
}
